package com.shennan.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shennan.commonutils.R;
import com.shennan.eduService.entity.EduTeacher;
import com.shennan.eduService.entity.vo.TeacherQuery;
import com.shennan.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * LTAIp9uz7nxZKuop
 * ND7WYJTvkCwLPhBBVJUdivG6fcU68U
 * @author sn
 * @since 2020-12-26
 */

@Api(description="讲师管理")        //swagger测试更加清晰
@RestController                   //@RestController 是@ResponseBody和@Controller的结合.@Controller 用来响应页面，表示当前的类为控制器。
@RequestMapping("/eduservice/teacher")      //作用：URL映射。
@CrossOrigin
public class EduTeacherController {


    //访问地址  http://localhost:8001/eduservice/teacher/findAll
    //注入service
    @Autowired
    private EduTeacherService teacherService;

    //1.查询讲师表中的所有数据
    @ApiOperation(value = "所有讲师列表")             //swagger测试更加清晰
    @GetMapping("findAll")
    public R findAllTeacher(){
        //调用service查询所有讲师
        List<EduTeacher> list = teacherService.list(null);
        //return list;
        return R.ok().data("items",list);
    }

    //@RequestParam注解给我们提供了这种解决方案，即允许用户不输入id时，使用默认值
    //@RequestParam(value="id",required = false,defaultValue = "1")

    //逻辑删除讲师的方法
    //id值需要通过路径来传递
    @ApiOperation(value = "根据ID删除讲师")             //swagger测试更加清晰
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)             //swagger测试更加清晰
            @PathVariable("id") String id){           //@PathVaribale 获取url中的数据
        boolean flag = teacherService.removeById(id);
        if(flag) return R.ok();
        else return R.error();
    }

    //3分页查询讲师方法
    //需要两个参数，那么要在地址里面体现出来

    /**
     *
     * @param current,当前页
     * @param limit，每页记录数
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit){

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法
        //封装的数据全都在pageTeacher里面
        teacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    //4条件查询+分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery     //用对象的形式，得到条件查询
                                ){      //required = false，表示查询条件可以为null

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //多条件组合查询
        //动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level) ) {
            wrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        //调用方法，实现条件查询
        teacherService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }


    //5添加讲师,post提交
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){

        boolean save = teacherService.save(eduTeacher);

        return save ? R.ok() : R.error();
    }

    //6根据讲师id进行查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);

        if(eduTeacher!=null){
            return R.ok().data("teacher",eduTeacher);
        }else{
            return R.error();
        }


    }

    //7修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);

        return flag ? R.ok() : R.error();
    }
}

